// global tools function
import { EXCLUDE_STORAGE_KEY } from '..//constants'

export const object2Str = (o : string|object): string => {
  return typeof o === 'string' ? o : JSON.stringify(o)
}

/*
节流函数
*/
export const throttle = function(func: Function, wait: number) {
  let previous = 0;
  return function(this: any) {
      let now = Date.now();
      let context = this;
      let args = arguments;
      if (now - previous > wait) {
          func.apply(context, args);
          previous = now;
      }
  }
}
/**
 * storage
 * 包含获取、设置、删除、清空方法
 * get(key: stirng): string
 * set(key: string, val: string): void
 * del(key: stirng): void
 * clear(): void
 */
export const storage = {
  get(key: string) {
    return localStorage.getItem(key) || '';
  },

  set(key: string, val: string) {
    localStorage.setItem(key, val);
  },

  del(key: string) {
    localStorage.removeItem(key);
  },

  clear(exclude?: boolean) {
    if(exclude) {
      Object.keys(localStorage).forEach(key => {
        if(!EXCLUDE_STORAGE_KEY.includes(key)){
          this.del(key);
        }
      })
    }else{
      localStorage.clear();
    }
  }
};

/**
 *
 */
export const getFullDate = (): string => {
  let date = new Date(),
		Y = date.getFullYear(),
		M = date.getMonth() + 1,
    D = date.getDate();

  let MM = (M < 10 ? '0' : '') + M,
    DD = (D < 10 ? '0' : '') + D;
  return Y + MM + DD
}
/*
  js加减乘除
*/
export const jsOperation = (num1: number, num2: number, type: string) => {
  let baseNum, baseNum1, baseNum2;
  try {
      baseNum1 = num1.toString().split('.')[1].length;
  } catch (e) {
      baseNum1 = 0;
  }
  try {
      baseNum2 = num2.toString().split('.')[1].length;
  } catch (e) {
      baseNum2 = 0;
  }
  baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
  if (type === 'add') {
    return (num1 * baseNum + num2 * baseNum) / baseNum;
  }else if (type === 'minus') {
    // let precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;
    return ((num1 * baseNum - num2 * baseNum) / baseNum);
  }else if(type === 'multi') {
    return Number(String(num1).replace('.', '')) * Number(String(num2).replace('.', '')) / Math.pow(10, baseNum1 + baseNum2);
  }else if(type === 'divi') {
    return (Number(num1.toString().replace('.', '')) / Number(num2.toString().replace('.', ''))) * Math.pow(10, baseNum2 - baseNum1);
  }else {
    return 0;
  }
}

/**
 * 获取浏览器类型
 */
export const getClientType = () => {
  const u = navigator.userAgent;
  if (u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
    return {type:'iOS', version: u.match(/iP[^;]+; CPU[^\d]+[\d+(_|.)]+/)};
  } else if (u.match(/Android/)) {
    return {type: 'Android', version: u.match(/Android ([\d.]+)/)![0]};
  } else if (u.match(/Trident/)) {
    return {type: 'IE', version: u.match(/(MSIE ([\d.]+))|(Trident\/[\d.]+)/)![0]};
  } else if (u.match(/Edge/)) {
    return {type: 'Edge', version: u.match(/Edge\/([\d.]+)/)![0]};
  } else if (u.match(/Chrome/)) {
    return {type: 'Chrome', version: u.match(/Chrome\/([\d.]+)/)![0]};
  } else if (u.match(/Firefox/)) {
    return {type: 'Firefox', version: u.match(/Firefox\/([\d.]+)/)![0]};
  } else if (u.match(/Safari/)) {
    return {type: 'Safari', version: u.match(/Safari\//)![0] + u.match(/Version\/([\d.]+)/)![1]};
  } else {
    return {type: u, version: null};
  }
}

/**
 * 获取现在的年月日等时间
 * Y:年, M:月, D:日, H:时, M:分, S:秒, s:毫秒, W:星期
 */
export const getNow = () => {
  const d = new Date()
  return {
    Y: d.getFullYear(),
    M: d.getMonth(),
    D: d.getDate(),
    H: d.getHours(),
    m: d.getMinutes(),
    S: d.getSeconds(),
    s: d.getMilliseconds(),
    W: d.getDay(),
  }
}
